﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;

namespace DYSales.Views.total
{
    public partial class total_page : Page
    {
        public total_page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(total_page_Loaded);
            bt_统计.Click += new RoutedEventHandler(bt_统计_Click);
            bt_打印.Click += new RoutedEventHandler(bt_打印_Click);
        }

        void bt_打印_Click(object sender, RoutedEventArgs e)
        {
            dm.getDS().GetSetTable(lo =>
            {
                if (!lo.HasError)
                {
                    PrintManager.tbs = new List<TextBlock>();
                    PrintManager.tbcount = 0;
                    var setting = lo.Value;
                    TextBlock tb = PrintManager.CreatePrintTextBlock(setting.s_打印页宽);
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小 + 1, FontWeight = FontWeights.Bold, Text = setting.s_店名 + "营业结算单" });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小, Text = "营业总金额：" + tbk_营业总金额.Text });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小, Text = "支出总金额：" + tbk_支出总金额.Text });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小, Text = "成本总金额：" + tbk_商品成本总金额.Text });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小, Text = "商品利润：" + tbk_商品利润结算.Text });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小, Text = "结算金额：" + tbk_结算.Text });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小, Text = "统计时间：" + tbk_统计时间.Text });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小, Text = "统计范围：" + tbk_统计范围.Text });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new Run() { FontSize = setting.s_字体大小, Text = "操作员：" + WebContext.Current.User.Name });
                    tb.Inlines.Add(new LineBreak());
                    PrintManager.tbs.Add(tb);
                    PrintManager.GetPrintManager().DoPrint();
                }
            }, null);
        }

        void bt_统计_Click(object sender, RoutedEventArgs e)
        {
            refresh();
        }

        void total_page_Loaded(object sender, RoutedEventArgs e)
        {
            dm.getDS().GetST(lt =>
            {
                dm.getDS().GetSetTable(ls =>
                {
                    var lttime = lt.Value;
                    var lstime = ls.Value.s_营业开始时间;
                    var lstime2 = ls.Value.s_营业结束时间;
                    tb_startTime.DateTime = new DateTime(lttime.Year, lttime.Month, lttime.Day, lstime.Hour, lstime.Minute, lstime.Second);
                    tb_lasttime.DateTime = new DateTime(lttime.Year, lttime.Month, lttime.Day, lstime2.Hour, lstime2.Minute, lstime2.Second).AddDays(1);
                }, null);
            }, null);
        }

        void refresh()
        {
            if (tb_lasttime.DateTime != null && tb_startTime.DateTime != null)
            {
                dm.getDS().GetTotalSellTablesByTime(tb_startTime.DateTime.Value, tb_lasttime.DateTime.Value, lo =>
                {
                    if (!lo.HasError)
                    {
                        g_统计.DataContext = lo.Value;
                    }
                }, null);
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }
}
